import 'dart:typed_data';

import 'package:cloudapp/esdk/response/file_list_item.dart';
import 'package:flutter/material.dart';
import 'dart:convert' as convert;

var picturePlaceHolder = Image.asset(
  'res/images/album.png',
  width: 100,
  height: 100,
  fit: BoxFit.none,
);

class PictureFileItem extends StatelessWidget {
  final FileListItem file;
  final Function onClick;
  PictureFileItem({Key key, @required this.file, @required this.onClick}):super(key: key);

  @override
  Widget build(BuildContext context) {
    // 如果有预览图，则加载，否则使用默认占位符
    Widget img;
    Uint8List bytes;
    if(file.thumb.isNotEmpty) {
      bytes = convert.base64Decode(file.thumb);
      img = Image.memory(
        bytes,
        width: 100,
        height: 100,
        fit: BoxFit.none,
      );
    } else {
      img = picturePlaceHolder;
    }
    return GestureDetector(
      onTap: () {
        onClick();
      },
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: <Widget>[
          img,
          Text(
            file.name,
            maxLines: 1,
            overflow: TextOverflow.ellipsis,
            textAlign: TextAlign.center,
            style: TextStyle(
              fontSize: 10.0,
            ),
          )
        ],
      ),
    );
  }
}
